<?php

/**
 * Authentication component
 *
 * Manages user logins and permissions.
 *
 * PHP 5
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       cake.libs.controller.components
 * @since         CakePHP(tm) v 0.10.0.1076
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */
class WatermarkComponent extends Component {


    public function __construct() {

    }

    public function getExt($filename) {
        $ext = substr(strstr($filename, '.'), 1);
        $check  = preg_match('@(?:.*)\.([^.]*)$@', $filename, $matchs);
        if($check){
            return strtolower($matchs[1]);
        }else{
            return false;
        }
    }
    public function createResource($image){
        $image_ext  = strtolower($this->getExt($image));
        switch ($image_ext){
            case 'jpg':
                $image_resource =   imagecreatefromjpeg($image);
                break;
            case 'jpeg':
                $image_resource =   imagecreatefromjpeg($image);
            break;
            case 'gif':
                $image_resource =   imagecreatefromgif($image);
            break;
            case 'png':
                $image_resource =   imagecreatefrompng($image);
        }
        if(is_resource($image_resource)){
            return $image_resource;
        }
        return false;
    }
    public function saveFile($image_resource, $path, $ext){
        switch ($ext){
            case 'jpg':
            case 'jpeg':
                $saved =   imagejpeg($image_resource, $path, 100);
            break;
            case 'gif':
                $saved =   imagegif($image_resource, $path);
            break;
            case 'png':
                $saved  =   $image_resource =   imagepng ($image_resource, $path, 100);
        }
    }
    public function watermark_center($image){
        $image_ext  = strtolower($this->getExt($image));
        $image_resource =   $this->createResource($image);

        if(is_resource($image_resource)){
            $image_size = getimagesize($image);
            $grey = imagecolorallocate($image_resource, 3, 29, 54);
            $font   =   CAKE_CORE_INCLUDE_PATH . DS . 'fonts' . DS . 'ARIALUNI.TTF';
            // Add the text
            imagettftext($image_resource, 13, 0, $image_size[0]/2 - 135, $image_size[1]/2 - 10, $grey, $font, 'Copyright ' . chr(169) .  ' www.thoitrangred.com');
            $this->saveFile($image_resource, $image, $image_ext);
            imagedestroy($image_resource);
        }
        return;
    }
    public function watermark($image){
        $image_ext  = strtolower($this->getExt($image));
        $image_resource =   $this->createResource($image);
        
        if(is_resource($image_resource)){
            $image_size = getimagesize($image);
            $grey = imagecolorallocate($image_resource, 3, 29, 54);
            $font   =   CAKE_CORE_INCLUDE_PATH . DS . 'fonts' . DS . 'ARIALUNI.TTF';
            // Add the text
            imagettftext($image_resource, 13, 0, 10, $image_size[1] - 10, $grey, $font, 'Copyright ' . chr(169) .  ' www.thoitrangred.com');
            $this->saveFile($image_resource, $image, $image_ext);
            imagedestroy($image_resource);
        }
        return;
    }
}

